package org.mlz.game.model

import com.sun.javafx.scene.traversal.Direction
import com.sun.javafx.scene.traversal.Direction.*
import org.itheima.kotlin.game.core.Painter
import org.mlz.game.Config
import org.mlz.game.business.Attackable
import org.mlz.game.business.Blockable
import org.mlz.game.business.Movable
import org.mlz.game.business.Sufferable

/**
 *<p>
 *我方坦克
 *
 * 具备移动能力
 * 具备阻挡能力
 *
 *</p>
 * CreateBy  Malingzhao
 * @date  2020/9/18
 */
class Tank(override var x: Int, override var y: Int) : Movable, Blockable, Sufferable {
    /**
     * 生命值
     */
    override var blood: Int = 20


    override val width: Int = Config.block
    override val height: Int = Config.block

    //方向
    override var currentDirection: Direction = UP

    //速度

    //坦克不可以走的方向
    private var badDirection: Direction? = null


    /**
     *
     * 判断移动五i是否和阻塞物体发生碰撞
     *@return 碰撞  要碰撞的方向
     */
    override fun willCollision(block: Blockable): Direction? {
        //将要碰撞的时候做判断

        //未来的坐标
        var x = this.x
        var y = this.y

        //将要碰撞的时候做判断
        when (currentDirection) {
            UP -> y -= speed
            DOWN -> y += speed
            LEFT -> x -= speed
            RIGHT -> x += speed

        }
        //TODO 检测碰撞

//        var collission = if (block.y + block.height <= y) {
//            //如果阻挡物在运动物上方 不碰撞
//            false
//        } else if (y + height <= block.y) {
//            //如果阻挡物在运动物的下方 不碰撞
//            false
//        } else if (block.x + block.width <= x) {
//            //左方
//            false
//        } else if (x + width <= block.x) {
//            //右方
//            false
//        } else {
//            //碰撞了
//            true
//        }
        var collission = checkCollision(block.x, block.y, block.width, block.height, x, y, width, height)
        return if (collission) currentDirection else null
    }

    override fun notifyCollisionBlock(direction: Direction?, block: Blockable?) {
        //TODO 接收碰撞信息
        this.badDirection = direction
    }

    //速度
    override val speed: Int = 64


    override fun draw() {

        //根据坦克的方向进行绘制
        //方式一
        //        when (currentDirection){
        //            Direction.UP -> Painter.drawImage("img/tank_u.gif",x,y)
        //            Direction.DOWN -> Painter.drawImage("img/tank_d.gif",x,y)
        //            Direction.LEFT -> Painter.drawImage("img/tank_l.gif",x,y)
        //            Direction.RIGHT -> Painter.drawImage("img/tank_r.gif",x,y)
        //        }
        val imgPath: String = when (currentDirection) {
            UP -> "img/tank_u.gif"
            DOWN -> "img/tank_d.gif"
            LEFT -> "img/tank_l.gif"
            RIGHT -> "img/tank_r.gif"
            NEXT -> TODO()
            NEXT_IN_LINE -> TODO()
            PREVIOUS -> TODO()
        }
        Painter.drawImage(imgPath, x, y)
    }

    /**
     * 坦克移动
     */
    fun move(direction: Direction) {
        //判断是否是往要碰撞的方向走
        if (direction == badDirection) {
            //不往下执行 不走了
            return
        }

        //当前的方向和徐娅移动的方向不一致的时候,智能做方向的改变
        if (this.currentDirection != direction) {
            this.currentDirection = direction
            return
        }

        this.currentDirection = direction

        //坦克的坐标需要发生变化
        //根据不同的方向,改变对应的坐标
        when (currentDirection) {
            UP -> y -= speed
            DOWN -> y += speed
            LEFT -> x -= speed
            RIGHT -> x += speed
        }
        //越界判断
        if (x < 0) x = 0;
        if (x > Config.gameWidth - width) x = Config.gameWidth - width
        if (y < 0) y = 0;
        if (y > Config.gameHeight - height) y = Config.gameHeight - height;

    }

    /**
     * 发射子弹的方法
     */
    fun shot(): Bullet {
        return Bullet(this,currentDirection, { bulletWidth, bulletHeight ->
            val tankX = x
            val tankY = y
            val tankWidth = width
            val tankHeight = height
            //计算子弹的真实的坐标
            //如果坦克是向上的
            //bulletX = tankX + (tankWidth - bulletWidth)/2
            //bulletY = tankY - bulletHeight /2

            var bulletX = 0 //x 默认值
            var bulletY = 0 //y 默认值
            var bulletWidth = 16 //不是写死的
            val bulletHeight = 32
            when (currentDirection) {
                UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2
                }
                DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2
                }
                LEFT -> {
                    bulletX = tankX - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }
                RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }
            }
            Pair(bulletX, bulletY)
        })
    }

    override fun notifySuffer(attackable: Attackable): Array<View>? {

        blood -= attackable.attackPower

        return arrayOf(Blast(x, y))
    }
}
